<?php
class Admin_Admin_AdminCommentController extends Zendvn_Controller_Action{
	
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH .'/admin/white');		
		$this->_templateObj->load();	
		
		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		$this->_langObj->setLangFile(array('module.comments.module.tmx','filter.toolbar.tmx'));		
		Zend_Registry::set('Zend_Translate',$this->_langObj->generate());
		
		/*====================================================
		 * Thiet lap cac gia tri cho session cua controller
		 *====================================================*/	
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
// 		$ss->unsetAll();
		if(empty($ss->col)){
			$ss->col = 'i.id';
			$ss->order = 'DESC';
		}
		$ss->setExpirationSeconds(600);
		$this->_arrParam['arrFilter'] = $ss->getIterator();
		
		/*====================================================
		 * Thiet lap gia tri cho phan phan trang cua controlle
		 *====================================================*/
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] = $pageNumber;
		$this->_paning['rowCount'] = $this->_generalObj->getSite('list_limit');
		$this->_paning['pageRange'] = $this->_generalObj->getSite('pageRange');					
		$this->_arrParam['panigator'] = $this->_paning;
		
		/**
		 * delete.all
		 * ------------------------------------
		 * khoi tao thong tin sInfo
		 */
		$sInfo = new Zendvn_System_Info();
		$this->_arrParam['sInfo'] = $sInfo->getMemberInfo();
		
		
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
		//Hien thi title
		$this->view->Title = $this->view->translate('Comments Manager') .' : '
							 .$this->view->translate('List');
		$this->view->headTitle($this->view->Title,true);
		
		$this->view->slbSearchFiels = array(
				'i.id'			=> $this->view->translate('- ID field'),
				'i.content'		=> $this->view->translate('- Content field'),
				'i.created'		=> $this->view->translate('- Created field'),
				'u1.user_name'	=> $this->view->translate('- Created by field'),
				'ip'			=> $this->view->translate('- IP field'),
		);
		
		$tblCom = new Comment_Model_Comment();
		$this->view->Items = $tblCom->listItem($this->_arrParam);
		
		$totalItem = $tblCom->totalItem($this->_arrParam);
		$panigator = Zendvn_Paginator::create($this->_arrParam['panigator'],$totalItem);		
		$this->view->panigator = $panigator;
		
		
	}
	
	public function editAction(){
		$this->view->Title = $this->view->translate('Comments Manager') .' : '
							 .$this->view->translate('Item') .' : '
							 .$this->view->translate('Edit');
		$this->view->headTitle($this->view->Title,true);
		
		$tblCom = new Comment_Model_Comment();
		$this->view->Item = $tblCom->getItem($this->_arrParam);
		if($this->_request->isPost()){			
			$validator = new Comment_Form_ValidateComment($this->_arrParam);
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item = $validator->getData();				
			}else{
				$data =  $validator->getData();				
				$result = $tblCom->saveItem($data,array('task'=>'admin-edit'));
				if($result){
					/**
					 * delete.all
					 * ------------------------------------
					 * LOG
					 */
					$lnk = $this->view->cmsUrl(array('module'=>'comment','controller'=>'admin-comment','action'=>'info','id'=>$data['id']));
					$log = $this->view->translate('%s updated the comment [id:%s]',Zendvn_System_Info::getMemberInfor('user_name'),'<a href="'.$lnk.'">'.$data['id'].'</a>');
					Zendvn_System_Activity::writeLog($log);
					
					$this->_helper->flashMessenger->addMessage(array('success'=>$this->view->translate('The update was successful')));
				}
				
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController.'/' 
							.$this->_arrParam['action'].'/id/' 
							.$this->_arrParam['id'].'/project/' 
							.$this->_arrParam['project']);
				}
			}
		}
		
		$this->_helper->viewRenderer('form');
	}
	
	
	
	public function deleteAction(){
				
		$tblCom = new Comment_Model_Comment();
		
			//kiem tra truong hop xoa khong phai la multi , chuyen id do vao dang muilti de xoa
			if(empty($this->_arrParam['type']) && $this->_arrParam['type'] != 'multi'){
				$this->_arrParam['cid'] = array($this->_arrParam['id']);
			}

			//lay ra tat ca cac comment_id cha con
			$result = $tblCom->getId($this->_arrParam);
	
			//xoa coment theo dang muilti
			if($this->_arrParam['type'] == 'multi'){
				$tblCom->deleteItem(array('cid'=>$result), array('task'=>'multi'));
			}else{
				
				//kiem tra xem cai comment_id dang xoa co con hay khong
				if(count($result>1)){
					// id dang xoa co comment_id cha con
					$tblCom->deleteItem(array('cid'=>$result), array('task'=>'multi'));
				}else{
					//id dang xoa khong co comment_id cha con
					$tblCom->deleteItem(array('id'=>$result));
				}
			}
		
		$this->_helper->flashMessenger->addMessage(array('success'=>$this->view->translate('The delete was successful')));
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
		
	}
	
	public function infoAction(){
		$this->view->Title = $this->view->translate('Comments Manager') .' : '
							 .$this->view->translate('Item') .' : '
							 .$this->view->translate('Info');
		$this->view->headTitle($this->view->Title,true);
		
		$tblCom = new Comment_Model_Comment();
		$this->view->Item = $tblCom->getItem($this->_arrParam,array('task'=>'info'));
	}
	
	
	
	public function statusAction(){
		
		$tblItem = new Comment_Model_Comment();
		
		if($this->_arrParam['type'] == 'multi'){
			$tblItem->statusItem($this->_arrParam,array('task'=>'multi'));
		}else{
			$tblItem->statusItem($this->_arrParam);
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	
	
	public function filterAction(){
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		
		if($this->_arrParam['type'] == 'search'){
			$ss->keyword = $this->_arrParam['keyword'];
			$ss->searchField = $this->_arrParam['search-field'];
		}
		if($this->_arrParam['type'] == 'filter'){
			$ss->status = $this->_arrParam['status'];
		}
		if($this->_arrParam['type'] == 'clear'){
			$ss->keyword = '';
			$ss->searchField = '';
		}
		if($this->_arrParam['type'] == 'sort'){
			$ss->col = $this->_arrParam['col'];
			$ss->order = $this->_arrParam['order'];
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
}